
package playground;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author badi
 */
public class Worker implements Runnable {
    String inet_file;
    int sock;

    public Worker(String f, int s){
        inet_file = f;
        sock = s;
    }

    public void run() {
        try {
            System.out.println("Worker running!");

            InetAddress addr = InetAddress.getLocalHost();
            Main.write(addr, new FileOutputStream(inet_file));

            ServerSocket server = new ServerSocket(sock);
            Socket client = server.accept();


            Callable f = (Callable) Main.read(client.getInputStream());
            System.out.println("Got work!");
            Main.write(f.call(), client.getOutputStream());
            

            server.close();
            client.close();


        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            System.out.println("Worker done!");
        }
    }


}
